var app = getApp();
var util = require("../../utils/libs.js")
var api = require("../../utils/api.js")
Page({
  data: {
    value:'',
    images: '',
    textNumber:'',
    numbercount:'',
    imgUrl:'',
    select:'',
    logo:'',
    uid:"",
    id:"",
    title:"",
    content:"",
    step:"",
    step_allow:"",
    step_time:"",
    is_draft:1
  },
  onLoad (options) {
    let that = this
    let uid= wx.getStorageSync('uid')
    // 页面初始化 options为页面跳转所带来的参数
  if(options.id){
    that.setData({
      uid:uid,
      id:options.id
    })
  } 
  that.setData({
    uid:uid
  })
  },
  getValue(event){
   let that = this
   let values = event.detail.value;
   that.setData({
     title: values
   })
  },
  getText(event){
    let that = this
    let values = event.detail.value;
    that.setData({
      content: values
    })
  },
  closeImages(){
    this.setData({
      imgUrl:''
    })
  },
  selectNum(){
    this.setData({
      select:{
        type:1,
        content:[300,500,700,900,1000]
      }
    })
  },
  closeS(){
    this.setData({
      select: ''
    })
  },
  selectV(event){
    var num = event.currentTarget.dataset.type;
    var val = event.currentTarget.dataset.val
    if(num==1){
      this.setData({
        textNumber:val,
        select:''
      })
    }else if(num==2){
      this.setData({
        step: val,
        select:''
      })
    }else if(num==3){
      this.setData({
        step_allow: val,
        select: ''
      })
    }else if(num==4){
      this.setData({
        step_time: val,
        select: ''
      })
    }
  },
  chooseImageTap (){
    let _this = this;
    wx.showActionSheet({
     itemList: ['从相册中选择', '拍照'],
     itemColor: "#f7982a",
     success: function(res) {
     if (!res.cancel) {
     if(res.tapIndex == 0){
     _this.chooseWxImage('album')
     }else if(res.tapIndex == 1){
     _this.chooseWxImage('camera')
     }
     }
     },
     fail: function(res) {
      console.log(res.errMsg)
    }
    })
},
chooseWxImage:function(type){
  let that = this;
  wx.chooseImage({
   sizeType: ['original', 'compressed'],
   sourceType: [type],
   success: function (res) {
   that.setData({
   logo: res.tempFilePaths[0],
   })
   app.upload_file("https://bucuo.jikeyun.net/api/upload",
    that.data.logo,"upload",{},function success(res){
        res = JSON.parse(res)
      if(res.error_code === 0){
        console.log(res)
        that.setData({
          imgUrl:res.data.file
        })
        console.log(that.data.imgUrl)
      }
    })
 }
})
},
  sendNumber(){
    this.setData({
      select: {
        type:2,
        content: [2, 4, 6, 8, 10, 12]
      }
    })
  },
  bindChange(e) {
    let that = this
    const number = e.detail.value
    that.setData({
      textNumber: number,
      state: false
    })
  },
  bindTime(e) {
    let that = this
    const number = e.detail.value
    that.setData({
      times: number,
      time: false
    })
  },
  sendTime() {
    this.setData({
      select:{
        type:4,
        content:['10min','30min','3h','1d']
      }
    })
  },
  sendPeople(){
    this.setData({
      select:{
        type:3,
        content:[2,6,8,10]
      } 
    })
  },
  addBuild(){
    let that = this;
    if(that.data.title ==''){
      wx.showToast({
        title: '请填写标题',
        icon:'none',
        duration: 2000,
      })
      return ;
    }
    if (that.data.content == '') {
      wx.showToast({
        title: '请填写文章内容',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (that.data.textNumber == '') {
      wx.showToast({
        title: '每期接龙字数',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (that.data.step == '') {
      wx.showToast({
        title: '每期接龙期数',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (that.data.step_allow == '') {
      wx.showToast({
        title: '每期接龙人数',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (that.data.step_time == '') {
      wx.showToast({
        title: '每期接龙时间',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    util.request(api.addBuild, {
      uid: that.data.uid,
      title: that.data.title,
      content: that.data.content,
      images: that.data.imgUrl,
      text_number: that.data.textNumber,
      step: that.data.step,
      step_allow: that.data.step_allow,
      step_time: that.data.step_time
    }, "POST")
      .then(function (res) {
        if (res.error_code === 0) {
          let id = res.data.id
          wx.showToast({
            title: '发布成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              wx.navigateTo({
                url: "/pages/listdetais/listdetais?id=" + id
              })
            }
          })
        }
      })
    
  },
  handleSave(){
    let that = this
    if (that.data.title == '') {
      wx.showToast({
        title: '请填写标题',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (that.data.content == '') {
      wx.showToast({
        title: '请填写文章内容',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (that.data.textNumber == '') {
      wx.showToast({
        title: '每期接龙字数',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (that.data.step == '') {
      wx.showToast({
        title: '每期接龙期数',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (that.data.step_allow == '') {
      wx.showToast({
        title: '每期接龙人数',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (that.data.step_time == '') {
      wx.showToast({
        title: '每期接龙时间',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    util.request(api.addBuild,{
      uid:that.data.uid,
      title:that.data.title,
      content:that.data.content,
      images:that.data.images,
      text_number: that.data.textNumber,
      step:that.data.step,
      step_allow:that.data.step_allow,
      step_time:that.data.step_time,
      is_draft:that.data.is_draft
    },"POST")
    .then(function (res) {
      if(res.error_code === 0){
        let id= res.data.id;
        console.log(id);
        setTimeout(function(){
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              wx.navigateTo({
                url: "/pages/solitaire/solitaire?id="+id
              })
            }
          })
        },3000)
        
      }
    })
  },
  onReady () {
    // 页面渲染完成
  },
  onShow () {
    // 页面显示
  },
  onHide () {
    // 页面隐藏

  },
  onUnload () {
    // 页面关闭

  }
})
